const db=wx.cloud.database()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fileList1:[],
    load:false,
    show:false,
    value:'',
    groupmember:[],
    group:[],
    userid:'',
    groupname:'',
  },
  onChange(event) {
    // event.detail 为当前输入的值
    this.setData({
      value:event.detail
    })
  },
  add(){
    let that=this
    if(this.data.value==''){
      wx.showToast({
        title: '组织id不为空',
        icon:"error"
      })
    }else{
        db.collection("group").where({
          _id:this.data.value
        }).get().then(res=>{
          if(res.data.length==0){
            wx.showToast({
              title: '没有该组织',
              icon:"error"
            })
          }else{

            that.setData({
              groupmember:res.data[0].groupmember,
              groupname:res.data[0].groupname
            })
            var st=true
            for(var index in that.data.groupmember)
                if(that.data.groupmember[index].userid==that.data.userid){
                  st=false;
                  break;
                }
            if(st){
              that.setData({
                  groupmember:that.data.groupmember.concat({userid:that.data.userid})
                })
                db.collection("group").doc(that.data.value).update({
                  data:{
                    groupmember:that.data.groupmember
                  }
                }).then(b=>{
                  db.collection("user").where({
                    _openid:that.data.userid
                  }).get().then(a=>{
                     that.setData({
                       group:a.data[0].group
                     })
                     wx.cloud.callFunction({
                       name:"useropenid",
                       data:{
                         openid:that.data.userid,
                         group:that.data.group.concat({groupid:that.data.value,groupname:that.data.groupname})
                       }
                     }).then(f=>{
                      wx.showToast({
                        title: '加入成功',
                      })
                      that.setData({
                        show:false
                      })
                     })
                      
                    
                  })
                  
                })
            }else{
              wx.showToast({
                title: '您已经在该组织',
                icon:"error"
              })
            }
            
          }
        })
    }
  },
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },
  afterRead(event) {
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    var id;
    let that=this;
    wx.cloud.callFunction({
      name:"openid"
    }).then(res=>{
      id=res.result.openid
    })
    wx.showLoading({
      title: '图片上传中...',
    })
    wx.cloud.uploadFile({
      cloudPath:Date.now()+".jpg",
      filePath:event.detail.file.url
    }).then(res=>{
      
        db.collection("user").where({
          _openid:id
        }).update({
          data:{
            url:res.fileID
          },
          success:a=>{
            that.setData({
              fileList1:[{url:res.fileID,isImage:true}]
            })
          }
        })
        wx.hideLoading({
          success: (res) => {},
        })
    })
  },
  
 
  delete(){
    this.setData({
      fileList1:[]
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    let that=this;
    wx.cloud.callFunction({
      name:"openid"
    }).then(res=>{
      this.setData({
        userid:res.result.openid
      })
      db.collection("user").where({
        _openid:res.result.openid
      }).get().then(e=>{
        
        if(e.data.length!=0){
          that.setData({
            load:true,
            fileList1:[{url:e.data[0].url,isImage:true}]
          })
         
        }
      })
    })
    wx.hideLoading({
      success: (res) => {},
    })
  },
  denglu(){
    let that=this;
    var id;
    wx.cloud.callFunction({
      name:"openid"
    }).then(res=>{
      id=res.result.openid
      
    })
    wx.getUserProfile({
      desc: 'qingqiu',
      success:m=>{
        
        db.collection("user").where({
          _openid:id
        }).get().then(res=>{
          if(res.data.length==0){
            
            db.collection("user").add({
              data:{
                nickName:m.userInfo.nickName,
                url:m.userInfo.avatarUrl
              }
              }).then(c=>{
                
              })
              that.setData({
                fileList1:[{url:m.userInfo.avatarUrl,isImage:true}],
                load:true
              })  
          }else{
            that.setData({
              fileList1:[{url:res.data[0].url,isImage:true}],
              load:true
            })
          }
        })
         
      }
    })
  },
  tuichu(){
    this.setData({
      fileList1:[],
      load:false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      fileList1:[]
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    /*db.collection("image").get()
    .then(res => {
      this.setData({
        fileList1:[{url:res.data[0].url}]
      })
      console.log(this.data.fileList1)
    })*/
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})